package com.lyx.bookmanager.controller.user;

import com.lyx.bookmanager.mapper.BookMapper;
import com.lyx.bookmanager.mapper.GxUserBookCollectMapper;
import com.lyx.bookmanager.mapper.RecordMapper;
import com.lyx.bookmanager.pojo.Book;
import com.lyx.bookmanager.pojo.GxUserBookCollect;
import com.lyx.bookmanager.pojo.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user/record")
public class UserRecordController {

    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    GxUserBookCollectMapper gxUserBookCollectMapper;
    @Autowired
    BookMapper bookMapper;

    @RequestMapping("/getOwnRecord")
    public String ownRecord(Model model, HttpSession session) {
        String username = (String) session.getAttribute("username");
        List<Record> records = recordMapper.getOwnRecord(username);
        model.addAttribute("records", records);
        return "user/ownRecord";
    }

    @RequestMapping("/addOwnRecord")
    public String addOwnRecord(Model model, HttpSession session, @RequestParam("bookid") String bookid) {
        String username = (String) session.getAttribute("username");
        long us = Long.parseLong(username);
        long bi = Long.parseLong(bookid);
        Record record = new Record();
        record.setBookId(bi);
        record.setReaderId(us);
        record.setLendDate(new Date());
        recordMapper.addOwnRecord(record);

        List<GxUserBookCollect> list = gxUserBookCollectMapper.getGxUserBookCollectList(bi, username);
        Book book = bookMapper.getBookById(bookid);
        book.setIsLend(1);
        book.setRemarkList(list);
        model.addAttribute("flag", 1);
        model.addAttribute("book", book);
        model.addAttribute("msg","借阅成功，返回原界面");
        return "user/addRecord_tips";
    }

    @RequestMapping("/returnBook")
    public String returnBook(Model model, HttpSession session, @RequestParam("bookid") String bookid) {
        long bi = Long.parseLong(bookid);
        String username = (String) session.getAttribute("username");
        long us = Long.parseLong(username);
        Record record = new Record();
        record.setBookId(bi);
        record.setReaderId(us);
        record.setBackDate(new Date());
        recordMapper.returnBook(record);

        List<GxUserBookCollect> list = gxUserBookCollectMapper.getGxUserBookCollectList(bi, username);
        Book book = bookMapper.getBookById(bookid);
        book.setIsLend(0);
        book.setRemarkList(list);
        model.addAttribute("flag", 1);
        model.addAttribute("book", book);
        model.addAttribute("msg", "退还成功，返回原界面");
        return "user/addRecord_tips";
    }
}
